/* eslint-disable no-unused-vars */
const _ = require('lodash');
const SpecialCharacter = require('../constant/index');

function rename (nameClass, parent) {
  let ableClass = nameClass;
  if (/(\d+)$/g.test(nameClass)) {
    ableClass = parent + ableClass.replace(RegExp.$1, '');
  }

  // 针对特殊字符进行添加前缀操作
  if (SpecialCharacter.includes(ableClass)) {
    ableClass = parent + ableClass;
  }

  return ableClass;
}

function distinct (__ob__, nameClass, parent, parameters = [], decorators = []) {
  if (!__ob__) throw new Error('数据函数对象错误，请检查后重试');

  const ableName = rename(nameClass, parent);
  const alphaClass = __ob__.get(ableName);

  if (alphaClass) {
    const intersection = _.intersection(alphaClass.intersection || alphaClass.decorators, decorators);
    alphaClass.intersection = intersection;
    alphaClass.decorators = _.union(alphaClass.decorators, decorators);
    alphaClass.parameters = _.union(alphaClass.parameters, parameters);
  } else {
    __ob__.set(ableName, {
      decorators,
      parameters
    });
  }

  return ableName;
}

module.exports = {
  rename,
  distinct
};
